from google.appengine.ext import db
from site_model import site_model
from advert import advert_model

class FeedSite():

    Name = 'Richard 4'
    # Feed URL
    FeedURL = ''
    # Host Header
    HostHeader = ''
    # IsValid
    IsValid = False
    # Dictionary of Adverts on this site
    Adverts = dict() 
    
    # Load the site based on the host url
    def Load(self, hosturl):
        # Load the Site record from the DB
        
        dbsite = site_model.all()
        dbsite.filter('hostheader = ', hosturl)
        
        self.HostHeader = hosturl
        
        if dbsite.count > 0:
            self.Name = dbsite[0].name
            self.FeedURL = dbsite[0].feedurl
                                  
            self.LoadAdverts(dbsite[0])

            self.IsValid = True
        else:
            self.IsValid = False
            
    def LoadAdverts(self, parent):       
        for ad in parent.adverts:
            self.Adverts[ad.position] = ad.content

           
    def TestSetup(self):
        x = site_model()
        x.hostheader = 'NEW FEED'
        x.name = 'NEW FEED'
        x.feedurl = 'http://pipes.yahoo.com/pipes/pipe.run?_id=5a0454d08207f2760c820cdba0e94b8b&_render=rss'
        x.put()
        
        ad = advert_model()
        ad.position = 'TOP'
        ad.content = 'TOP Advert Position'
        ad.site = x        
        ad.put() 
        
        ad2 = advert_model()
        ad2.position = 'INLINE'
        ad2.content = 'INLINE Advert Position'
        ad2.site = x        
        ad2.put()  
        
        ad3 = advert_model()
        ad3.position = 'LEFT'
        ad3.content = 'LEFT Advert Position'
        ad3.site = x        
        ad3.put()    

        ad4 = advert_model()
        ad4.position = 'RIGHT'
        ad4.content = 'RIGHT Advert Position'
        ad4.site = x        
        ad4.put()             